﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMS.DeDuplicator.BusinessLayer.Command;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.ApplicationLayer.State
{
    public class State : IState
    {
        IState _defaultState;
        Dictionary<Keys, Transition> _transitions;

        public State()
        {
            // default is to remain in current state if input not accounted for
            _defaultState = this;
        }

        public State(IState defaultState)
        {
            InputValidator.ValidateNotNull(_defaultState);
            _defaultState = defaultState;
        }

        public void AddTransition (Keys key, IState nextState, Command command)
        {
            InputValidator.ValidateNotNull(nextState);
            AddTransition(key, new Transition(nextState, command));
        }

        public void AddTransition(Keys key, Transition t)
        {
            InputValidator.ValidateNotNull(t);
            if (_transitions.ContainsKey(key))
                throw new Exception("already have transition for specified key");

            _transitions.Add(key, t);
        }

        public IState ProcessInput(Keys key)
        {
            if (!_transitions.ContainsKey(key))
                return _defaultState;

            return _transitions[key].ExecuteTransition();
        }
    }
}
